package com.zzt.controller;

import com.zzt.dao.MemberinfoMapper;
import com.zzt.entity.MemberAndMessage;
import com.zzt.entity.Memberinfo;
import com.zzt.entity.MemberinfoExample;
import com.zzt.service.MemberInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@CrossOrigin
@Controller
public class MemberInfoController extends HttpServlet {
    @Autowired
    MemberInfoService memberInfoService;
    @Resource
    MemberinfoMapper memberinfoMapper;
    @PostMapping(value = "/register")
    @ResponseBody
    public MemberAndMessage regist(@RequestParam("nickname") String nickname,
           @RequestParam("pwd") String pwd,
           @RequestParam("tel") String tel,@RequestParam("email") String email){
        //截取“=”号后面的
        int beginIndex1 = nickname.indexOf("=")+1;
        nickname = nickname.substring(beginIndex1);
        int beginIndex2 = pwd.indexOf("=")+1;
        pwd = pwd.substring(beginIndex2);
        int beginIndex3 = tel.indexOf("=")+1;
        tel = tel.substring(beginIndex3);
        int beginIndex4 = email.indexOf("=")+1;
        email = email.substring(beginIndex4);

        Memberinfo memberinfo = new Memberinfo(null, nickname, pwd, tel, email, "ROLE_LOGED", 1, null);
        MemberAndMessage memberAndMessage = memberInfoService.registerMember(memberinfo);
        return memberAndMessage;
    }

    @RequestMapping("/exitLogin")
    @ResponseBody
    public int exitLogin(HttpServletRequest request){
        request.getSession().invalidate();
        return 1;
    }

    @PostMapping("/loginPage")
    @ResponseBody
    public MemberAndMessage login(@RequestParam("nickname") String nickname,
         @RequestParam("pwd") String pwd, HttpServletRequest request){
        /*//截取“=”号后面的
        int beginIndex1 = nickname.indexOf("=")+1;
        nickname = nickname.substring(beginIndex1);
        int beginIndex2 = pwd.indexOf("=")+1;
        pwd = pwd.substring(beginIndex2);*/

        MemberAndMessage memberAndMessage = memberInfoService.loginMember(nickname, pwd);
        if(memberAndMessage.getMemberinfo()!=null){
            HttpSession session = request.getSession();
            session.setAttribute("user",memberAndMessage.getMemberinfo());
        }
        return memberAndMessage;
    }

    //是否已登录
    @RequestMapping("/ifLogined")
    @ResponseBody
    public MemberAndMessage ifLogined(HttpServletRequest request){

        //获取上下栈
        SecurityContext context = SecurityContextHolder.getContext();
        Authentication authentication1 = context.getAuthentication();
        String name = null;
        try {
            User principal = (User)authentication1.getPrincipal();
            name = principal.getUsername();
        } catch (Exception exception) {
            System.out.println("未登录");
        }

        MemberAndMessage memberAndMessage;
        if(name==null){
            memberAndMessage = new MemberAndMessage(null, 200, "您好！请登录");
        }else{
            HttpSession session = request.getSession();
            Memberinfo user = (Memberinfo)session.getAttribute("user");
            if(user!=null){ //存过user session
                return new MemberAndMessage(user, 400, "尊敬的用户: 【'  '】,您好!");
            }
            //如果没存过user session
            MemberinfoExample example = new MemberinfoExample();
            MemberinfoExample.Criteria criteria = example.createCriteria();
            criteria.andNicknameEqualTo(name);
            List<Memberinfo> memberinfos = memberinfoMapper.selectByExample(example);
            session.setAttribute("user",memberinfos.get(0));
            memberAndMessage = new MemberAndMessage(memberinfos.get(0), 400, "尊敬的用户: 【'  '】,您好!");
        }
        return memberAndMessage;
    }

    @GetMapping(value = "/user/UpdatePer")
    @ResponseBody
    public int UpdatePer(@RequestParam("nickname") String nickname,
             @RequestParam("pwd") String pwd,@RequestParam("tel") String tel,
            @RequestParam("email") String email,@RequestParam("Address") String Address,
                                HttpServletRequest request){

        HttpSession session = request.getSession();
        Memberinfo user = (Memberinfo)session.getAttribute("user");
        //
        Memberinfo memberinfo=null;
        memberinfo = new Memberinfo(user.getMno(), nickname, pwd,tel, email, "ROLE_LOGED", 1, Address);
        int i = memberInfoService.updatePersonInfo(memberinfo,user.getPwd());
        return i;
    }

    //展示信息
    @RequestMapping("/user/showMemberInfo")
    @ResponseBody
    public MemberAndMessage showMemberInfo(HttpServletRequest request){
        HttpSession session = request.getSession();
        Memberinfo user = (Memberinfo)session.getAttribute("user");
        MemberAndMessage memberAndMessage;
        if(user==null){
            return new MemberAndMessage(null, 200, "您好！请登录");
        }else{
            memberAndMessage = memberInfoService.showMemberInfo(user.getMno());
        }
        //展示完应该重新设session
        session.setAttribute("user",memberAndMessage.getMemberinfo());
        return memberAndMessage;
    }

    @RequestMapping("/toLogin")
    public String toLogin(){
        System.out.println("去登录");
//        response.sendRedirect("/loginPage.html");
        return "loginPage.html";
    }

    @RequestMapping("getss")
    public Object gets(HttpServletRequest request){
        //获取上下栈
        SecurityContext context = SecurityContextHolder.getContext();
        Authentication authentication1 = context.getAuthentication();
        User principal = (User)authentication1.getPrincipal();
        String name = principal.getUsername();

        if(name!=null){
            HttpSession session = request.getSession();
            MemberinfoExample example = new MemberinfoExample();
            MemberinfoExample.Criteria criteria = example.createCriteria();
            criteria.andNicknameEqualTo(name);
            List<Memberinfo> memberinfos = memberinfoMapper.selectByExample(example);
            session.setAttribute("user",memberinfos.get(0));
        }
        return principal;
    }
}
